import { defineStore } from 'pinia'
import { getLoadingConfig, getNavConfig, getShareConfig } from '@/apis/config'
import { useWxLogin } from "@/apis/user"
import { equ } from '@env'
import { isWechat } from '@/utils/client'
import { env } from '../../package-env'

export interface Style {
  name?: string
  x?: string | number
  y?: string | number
  font_size?: string | number
  backend_color?: string
  font_color?: string
}

export interface GuidanceImage {
  url?: string
  text?: string
}

export interface Config {
  name?: string
  logo?: string
  loading_switch_speed?: number
  loading_type?: number
  loading_video_url?: string
  loading_animation_url?: string
  style?: Style
  use_style?: boolean
  nav_is_show?: boolean
  logo_is_show?: boolean
  loading_image_list?: Record<string, string>[]
  guidance_image?: GuidanceImage
  navs: Record<string, LooseObject>[]
  share_info: Record<string, string>
  wechat_subscription: Record<string, string>
  clear_screen: string
  complete: boolean
  just_in_wechat: boolean
  switchMode: 'nav' | 'menu'
  currentUser: number
}

export const useConfigStore = defineStore({
  id: 'configStore',
  state: (): Config => ({
    name: '',
    logo: '',
    style: {},
    use_style: true,
    nav_is_show: false,
    logo_is_show: false,
    loading_type: 1,
    loading_switch_speed: 3,
    loading_image_list: [],
    loading_video_url: '',
    loading_animation_url: '',
    guidance_image: {},
    navs: [],
    share_info: {},
    wechat_subscription: {},
    clear_screen: '',
    complete: false,
    just_in_wechat: false,
    switchMode: 'nav',
    currentUser: 0,
  }),
  getters: {
    // loading配置
    swiper: (state: Config) => {
      const { loading_switch_speed = 3, loading_image_list = [] } = state
      return {
        speed: loading_switch_speed * 1000,
        list: loading_image_list,
      }
    },
    // 引导页配置
    guidance: (state: Config) => {
      return state.guidance_image
    },
    // 字幕配置
    captions: (state: Config) => {
      const { style = {} as Style, use_style } = state
      if (!use_style) return {}
      const fz = style.font_size ? (style.font_size > 20 ? 20 : style.font_size) : 12
      return {
        fontSize: `${fz}px`,
        fontColor: style.font_color || '#ffffffff',
        backgroundColor: style.backend_color || '#384472A6',
        bottom: style.y ? style.y + 'vh' : undefined,
      }
    },
    // 分享配置
    share: (state: Config) => state.share_info,
    // 订阅配置
    subscription: (state: Config) => state.wechat_subscription,
    // 清屏配置
    clearScreen: (state: Config) => {
      const [nav = '0', foot = '0', menu = '0'] = state.clear_screen.split('-')
      return { navFlag: nav === '1', footFlag: foot === '1', menuFlag: menu === '1' }
    }
  },
  actions: {
    // 获取loading配置信息
    async getLoadingConfig() {
      const res = await getLoadingConfig()
      document.title = res.name
      this.$state = { ...res, complete: true, currentUser: res?.index || 0 }
      if ((res.just_in_wechat && equ === 'h5' || isWechat())) {
        useWxLogin()
      }
      return Promise.resolve(res)
    },
    // 获取导航配置信息
    async getNavConfig() {
      if (this.navs.length > 0) return
      const { navigations = [] } = await getNavConfig()
      this.navs = navigations
    },
    // 获取分享信息
    async getShareConfig(content_id?: string) {
      const res = await getShareConfig(content_id)
      this.share_info = res
      return Promise.resolve(res)
    },
  },
})
